package com.dev.dream.exochatlockscreen;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.util.DisplayMetrics;

import com.dev.dream.libs.preperences.CustomCheckBoxPreference;
import com.dev.dream.libs.preperences.IconPreference;
import com.dev.dream.libs.share.MyShare;

public class SettingsActivity extends PreferenceActivity implements
		OnPreferenceChangeListener {

	private SharedPreferences pre;
	int height;
	int width;

	IconPreference share;
	Preference preview, changePin;

	CustomCheckBoxPreference display_date, display_time, enable_sound,
			enable_vibrator, enable_toolbar, enable_icon_toolbar,
			enable_passcode, enable_lockscreen, enable_save_mode, enable_sms,
			enable_miss_call, enable_hide_content_sms;

	// EditTextPreference text_to_unlock;

	ListPreference soundEffets;

	ListPreference list_time_format;

	String click_ad1 = "ad1_28082014";
	String click_ad2 = "ad2_25072014";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		getPreferenceManager().setSharedPreferencesMode(0);

		addPreferencesFromResource(R.xml.preferences);

		// switch (MathuUntil.random(1, 3)) {
		// case 1:
		// AdFlex.showGift(getApplicationContext(), AdFlex.GIFT_BOTTOM_LEFT);
		// break;
		//
		// default:
		// AdFlex.showGift(getApplicationContext(), AdFlex.GIFT_BOTTOM_RIGHT);
		// break;
		//
		// }

		// AdFlex.showGift(getApplicationContext(), AdFlex.GIFT_BOTTOM_RIGHT);

		pre = getSharedPreferences(
				"istudio.app.s5keypadlockscreen.slidemenu",
				MODE_PRIVATE);

		DisplayMetrics displaymetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay()
				.getMetrics(displaymetrics);
		height = displaymetrics.heightPixels;
		width = displaymetrics.widthPixels;

		preview = (Preference) findPreference("preview");
		preview.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			public boolean onPreferenceClick(Preference preference) {

				Intent i = new Intent(getApplicationContext(),
						LockScreenAppActivity.class);
				startActivity(i);

				return true;
			}
		});

		preview.setEnabled(EXOChatKeypadPreferences.getInstance()
				.isEnableLockscreen());

		changePin = (Preference) findPreference("changePasscode");
		changePin.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			public boolean onPreferenceClick(Preference preference) {

				Intent intent = new Intent(getApplicationContext(),
						ConfirmPasscodeActivity.class);
				intent.putExtra("CONFIRM", "CHANGE_PIN");
				startActivity(intent);

				return true;
			}
		});

		if (!EXOChatKeypadPreferences.getInstance().isEnablePasscode()) {
			changePin.setEnabled(false);
		}

		enable_lockscreen = (CustomCheckBoxPreference) findPreference("enableLockscreen");
		enable_lockscreen.setChecked(EXOChatKeypadPreferences.getInstance()
				.isEnableLockscreen());
		enable_lockscreen.setOnPreferenceChangeListener(this);

		display_date = (CustomCheckBoxPreference) findPreference("checkShowDate");
		display_date.setChecked(EXOChatKeypadPreferences.getInstance()
				.isDisplayDate());
		display_date.setOnPreferenceChangeListener(this);

		display_time = (CustomCheckBoxPreference) findPreference("checkShowTime");
		display_time.setChecked(EXOChatKeypadPreferences.getInstance()
				.isDisplayTime());
		display_time.setOnPreferenceChangeListener(this);

		enable_sound = (CustomCheckBoxPreference) findPreference("enable_sound");
		enable_sound.setChecked(EXOChatKeypadPreferences.getInstance()
				.isSound());
		enable_sound.setOnPreferenceChangeListener(this);

		enable_vibrator = (CustomCheckBoxPreference) findPreference("enable_vibrator");
		enable_vibrator.setChecked(EXOChatKeypadPreferences.getInstance()
				.isVibrator());
		enable_vibrator.setOnPreferenceChangeListener(this);

		list_time_format = (ListPreference) findPreference("listTimeFormat");
		list_time_format.setValueIndex(Integer.valueOf(EXOChatKeypadPreferences
				.getInstance().getTimeFormat()));
		list_time_format.setSummary(list_time_format.getEntry());
		list_time_format.setOnPreferenceChangeListener(this);

		enable_toolbar = (CustomCheckBoxPreference) findPreference("enable_toolbar");
		enable_toolbar.setChecked(EXOChatKeypadPreferences.getInstance()
				.isToolbar());
		enable_toolbar.setOnPreferenceChangeListener(this);

		enable_icon_toolbar = (CustomCheckBoxPreference) findPreference("enable_icon_toolbar");
		enable_icon_toolbar.setChecked(EXOChatKeypadPreferences.getInstance()
				.isIconToolbar());
		enable_icon_toolbar.setOnPreferenceChangeListener(this);

		enable_passcode = (CustomCheckBoxPreference) findPreference("enablePasscode");
		enable_passcode.setChecked(EXOChatKeypadPreferences.getInstance()
				.isEnablePasscode());
		enable_passcode.setOnPreferenceChangeListener(this);

		enable_save_mode = (CustomCheckBoxPreference) findPreference("enable_save_mode");
		enable_save_mode.setChecked(EXOChatKeypadPreferences.getInstance()
				.isEnableSaveMode());
		enable_save_mode.setOnPreferenceChangeListener(this);

		enable_sms = (CustomCheckBoxPreference) findPreference("enable_notifi_sms");
		enable_sms.setChecked(EXOChatKeypadPreferences.getInstance()
				.isEnableSMS());
		enable_sms.setOnPreferenceChangeListener(this);

		enable_miss_call = (CustomCheckBoxPreference) findPreference("enable_notifi_miss_call");
		enable_miss_call.setChecked(EXOChatKeypadPreferences.getInstance()
				.isEnableMissCall());
		enable_miss_call.setOnPreferenceChangeListener(this);

		enable_hide_content_sms = (CustomCheckBoxPreference) findPreference("enableHideSMS");
		enable_hide_content_sms.setChecked(EXOChatKeypadPreferences
				.getInstance().isHideContentSms());
		enable_hide_content_sms.setOnPreferenceChangeListener(this);

		soundEffets = (ListPreference) findPreference("soundEffets");
		soundEffets.setValueIndex(EXOChatKeypadPreferences.getInstance()
				.getIndexSound() - 1);
		soundEffets.setSummary(soundEffets.getEntry());
		soundEffets.setOnPreferenceChangeListener(this);

		if (EXOChatKeypadPreferences.getInstance().isEnableSaveMode()) {

			enable_sound.setEnabled(false);
			enable_vibrator.setEnabled(false);
		}

		share = (IconPreference) findPreference("share");
		share.setOnPreferenceClickListener(new OnPreferenceClickListener() {

			@Override
			public boolean onPreferenceClick(Preference preference) {
				// TODO Auto-generated method stub
				MyShare.shareTextUrl(SettingsActivity.this,
						getString(R.string.message_share),
						getString(R.string.subject_share));
				return false;
			}
		});

		Preference more = (Preference) findPreference("more");
		more.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			public boolean onPreferenceClick(Preference preference) {

				// Use your own code here
				Intent i = new Intent(
						Intent.ACTION_VIEW,
						Uri.parse("https://play.google.com/store/apps/developer?id=iptit"));
				startActivity(i);

				return true;
			}
		});

		Preference support = (Preference) findPreference("support");
		support.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			public boolean onPreferenceClick(Preference preference) {

				// Use your own code here
				Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri
						.fromParts("mailto", "iapp.ptit@gmail.com", null));
				emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
						getString(R.string.subject_email));

				startActivity(Intent.createChooser(emailIntent, ""));

				return true;
			}
		});

	}

	private void openUrl(String url) {
		try {
			startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
		} catch (Exception e) {

		}
	}

	@Override
	public boolean onPreferenceChange(Preference preference, Object newValue) {

		if (preference == display_date) {
			EXOChatKeypadPreferences.getInstance().setDisplayDate(
					(Boolean) newValue);
			display_date.setChecked(EXOChatKeypadPreferences.getInstance()
					.isDisplayDate());

			return true;
		} else if (preference == display_time) {
			EXOChatKeypadPreferences.getInstance().setDisplayTime(
					(Boolean) newValue);

			display_time.setChecked(EXOChatKeypadPreferences.getInstance()
					.isDisplayTime());

			return true;
		} else if (preference == enable_lockscreen) {
			EXOChatKeypadPreferences.getInstance().setEnableLockscreen(
					(Boolean) newValue);

			// if (EXOChatKeypadPreferences.getInstance().isEnableLockscreen())
			// {
			// startService(
			// new Intent(getApplicationContext(), SLockscreen.class));
			// } else {
			// stopService(
			// new Intent(getApplicationContext(), SLockscreen.class));
			// }

			enable_lockscreen.setChecked(EXOChatKeypadPreferences.getInstance()
					.isEnableLockscreen());

			preview.setEnabled(EXOChatKeypadPreferences.getInstance()
					.isEnableLockscreen());

			return true;
		} else if (preference == enable_sound) {
			EXOChatKeypadPreferences.getInstance().setSound((Boolean) newValue);

			enable_sound.setChecked(EXOChatKeypadPreferences.getInstance()
					.isSound());

			return true;
		} else if (preference == list_time_format) {

			EXOChatKeypadPreferences.getInstance().setTimeFormat(
					Integer.parseInt(newValue.toString()));

			list_time_format.setValueIndex(EXOChatKeypadPreferences
					.getInstance().getTimeFormat());
			list_time_format.setSummary(list_time_format.getEntry());

			return true;
		} else if (preference == enable_vibrator) {
			EXOChatKeypadPreferences.getInstance().setVibrator(
					(Boolean) newValue);

			enable_vibrator.setChecked(EXOChatKeypadPreferences.getInstance()
					.isVibrator());

			return true;
		} else if (preference == enable_toolbar) {
			EXOChatKeypadPreferences.getInstance().setToolbar(
					(Boolean) newValue);

			enable_toolbar.setChecked(EXOChatKeypadPreferences.getInstance()
					.isToolbar());

			return true;
		} else if (preference == enable_icon_toolbar) {
			EXOChatKeypadPreferences.getInstance().setIconToolbar(
					(Boolean) newValue);
			enable_icon_toolbar.setChecked(EXOChatKeypadPreferences
					.getInstance().isIconToolbar());

			return true;
		} else if (preference == enable_passcode) {

			// if
			// (S5KeypadPreferences.getInstance().getValuePasscode().equals(""))
			// {
			// Intent intent = new Intent(getApplicationContext(),
			// SetPasscodeActivity.class);
			// startActivity(intent);
			//
			// S5KeypadPreferences.getInstance().setEnablePasscode(
			// (Boolean) newValue);
			// enable_passcode.setChecked(S5KeypadPreferences.getInstance()
			// .isEnablePasscode());
			// }else{
			if (EXOChatKeypadPreferences.getInstance().isEnablePasscode()) {
				Intent intent = new Intent(getApplicationContext(),
						ConfirmPasscodeActivity.class);
				intent.putExtra("CONFIRM", "DISABLE_PIN");
				startActivity(intent);
			} else {
				Intent intent = new Intent(getApplicationContext(),
						SetPasscodeActivity.class);
				startActivity(intent);

				EXOChatKeypadPreferences.getInstance().setEnablePasscode(
						(Boolean) newValue);
				enable_passcode.setChecked(EXOChatKeypadPreferences
						.getInstance().isEnablePasscode());
			}
			// }

			return true;
		} else if (preference == enable_save_mode) {
			EXOChatKeypadPreferences.getInstance().setEnableSaveMode(
					(Boolean) newValue);
			enable_save_mode.setChecked(EXOChatKeypadPreferences.getInstance()
					.isEnableSaveMode());

			if (EXOChatKeypadPreferences.getInstance().isEnableSaveMode()) {

				enable_sound.setEnabled(false);
				enable_vibrator.setEnabled(false);
			} else {

				enable_sound.setEnabled(true);
				enable_vibrator.setEnabled(true);
			}

			return true;
		} else if (preference == enable_sms) {
			EXOChatKeypadPreferences.getInstance().setEnableSMS(
					(Boolean) newValue);
			enable_sms.setChecked(EXOChatKeypadPreferences.getInstance()
					.isEnableSMS());

			return true;
		} else if (preference == enable_miss_call) {
			EXOChatKeypadPreferences.getInstance().setEnableMissCall(
					(Boolean) newValue);
			enable_miss_call.setChecked(EXOChatKeypadPreferences.getInstance()
					.isEnableMissCall());

			return true;
		} else if (preference == soundEffets) {
			EXOChatKeypadPreferences.getInstance().setIndexSound(
					Integer.parseInt(newValue.toString()));
			soundEffets.setValueIndex(EXOChatKeypadPreferences.getInstance()
					.getIndexSound() - 1);

			soundEffets.setSummary(soundEffets.getEntry());

			return true;
		} else if (preference == enable_hide_content_sms) {
			EXOChatKeypadPreferences.getInstance().setHideContentSms(
					(Boolean) newValue);
			enable_hide_content_sms.setChecked(EXOChatKeypadPreferences
					.getInstance().isHideContentSms());

			return true;
		}

		return false;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		// Log.d("Kai", "Resume: " +
		// S5KeypadPreferences.getInstance().isEnablePasscode());
		enable_passcode.setChecked(EXOChatKeypadPreferences.getInstance()
				.isEnablePasscode());

		if (EXOChatKeypadPreferences.getInstance().isEnablePasscode()) {
			changePin.setEnabled(true);
		} else {
			changePin.setEnabled(false);
		}
		super.onResume();
	}
}
